<?php
namespace miniapp\cpmin\validate;

use miniapp\cpmin\service\CplistService;
use think\facade\Log;
use think\Validate;

class ApiValidate extends Validate{
    protected $rule=[
        'name'=>'require',
        'tel'=>'require|mobile',
        'code'=> ['require','checkCode'=>true]
    ];
    protected $message = [
        'name.require' => '名称必须',
        'tel.require'  => '电话必须',
        'tel.mobile'   => '合规手机号',
        'code.require' => '必须扫码',
//        'code.checkLength' => '条码不合规',
        'code.checkCode' => '条码已使用'
    ];
    protected $scene = [
        'scan'  =>  ['name','tel','code'],
        'scanCode'  =>  ['code']
    ];
    //验证条码长度
    protected function checkLength($value,$rule,$data=[])
    {
        return strlen($value)===$rule;
    }
    //验证福彩条码是否使用
    protected function checkCode($value,$rule,$data=[]){
        $cplistS = new CplistService();
        return $cplistS->isExsitCode($value) !== $rule;
    }
}
